<template>
	<view class="body">
		<view class="menu-wrapper">
			<view class="mitem-wrapper">
				<view class="m_icowrapper">
					<image class="m_ico" src="../../static/icons/icon_android.png"></image>
				</view>	
			</view>
			<view class="mitem-wrapper">
				<view class="m_icowrapper">
					<image class="m_ico" src="../../static/icons/icon_cloud_sync.png"></image>
				</view>
			</view>
			<view class="mitem-wrapper">
				<view class="m_icowrapper">
					<image class="m_ico" src="../../static/icons/icon_server.png"></image>
				</view>
			</view>
		</view>
		<view class="menu-wrapper">
			<view class="menu-title">远端数据</view>
		</view>
		<view class="menu-wrapper">
			<view class="chk-wrapper">
				<label for="chk1" @tap="Ckchange('1')">
					<checkbox id="chk1" v-model="basic_data"></checkbox> 基础数据
				</label>				
				{{basic_count}}
			</view>
		</view>
		<view class="menu-wrapper">
			<view class="chk-wrapper">
				<label for="chk2" @tap="Ckchange('2')">
					<checkbox id="chk2" v-model="store_data"></checkbox> 仓储货位
				</label>
				{{store_count}}
			</view>
		</view>
		<view class="menu-wrapper">
			<view class="menu-title">本地数据</view>
		</view>
		<scroll-view scroll-y="true" style="height: 250px;">
			<view class="menu-wrapper-list" v-for="(item,index) in local_data" :key="index">
				<view class="seq-wrapper font-sm">
					{{index+1}}
				</view>
				<view class="content-wrapper">
					<view class="order-wrapper font-lg">
						{{item.code}}
					</view>
					<view class="code-wrapper font-sm">
						{{item.inventlocdef_code}}
					</view>
				</view>
				<view class="date-wrapper font-sm">
					<view class="user-wrapper">
						{{item.create_who}}
					</view>
					<view class="month-wrapper">
						{{item.create_date | formatTime('yyyy-MM-dd')}}
					</view>
					<view class="time-wrapper">
						{{item.create_date | formatTime('hh:mm:ss')}}
					</view>
				</view>
			</view>
		</scroll-view>		
		<view class="footer-wapper menu-wrapper">
			<view class="btn-wrapper">				
				<button type="primary" @tap="syndata" :disabled="is_canuse">同步数据</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				basic_data:false,
				store_data:false,
				basic_count:'',
				store_count:'',
				local_data:[],
				end:'0',
				is_canuse:false
			}
		},
		onLoad() {
			var _this=this;
			uni.getNetworkType({
				success: (res) => {
					if(res.networkType!='wifi'){
						// uni.showToast(this.$config.msgs.needwifi)
						uni.showToast({
							title:this.$config.msgs.needwifi,
							icon:'none',
							duration:1500
						})
						_this.is_canuse=true
					}
				}
			})			
		},
		onShow() {
			var _this=this;
			var data={}
			_this.$http.syncount(data).then(res=>{				
				if(res.code=='1'){
					_this.basic_count=res.data.user
					_this.store_count=res.data.invent
				}
				_this.$local.ScanHistory((res)=>{
					var array=JSON.parse(res.data);
					if(array.length>0){
						this.local_data=array;
					}
				},(res)=>{
					console.log(res)
				})
			})
		},
		filters:{
			formatTime: function (date, fmt) {
				var date = new Date(date);
				if (/(y+)/.test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
				}
				var o = {
					'M+': date.getMonth() + 1,
					'd+': date.getDate(),
					'h+': date.getHours(),
					'm+': date.getMinutes(),
					's+': date.getSeconds()
				};
				for (var k in o) {
					if (new RegExp('('+k+')').test(fmt)) {
						var str = o[k] + '';
						fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length));
					}
				}
				return fmt;
			}		
		},		
		methods: {	
			Ckchange(f){
				if(f=="1")
					this.basic_data=!this.basic_data
				else
					this.store_data=!this.store_data
			},
			syndata(){
				var _this=this;
				if(_this.$config.SysConfig.APP_ISONLINE){
					_this.$common.isWifiExec(()=>{
						uni.showLoading({
						    title: _this.$config.msgs.isSyncing
						});
						// var _this=this;
						var download="";
						if(_this.basic_data && _this.store_data){
							download="3";
						}
						else if(_this.basic_data){
							download="1";}
						else if(_this.store_data){
							download="2";
						}
						var end="0";
						var count=parseInt((_this.local_data.length +(_this.$config.SysConfig.once_uploadCount-1))/_this.$config.SysConfig.once_uploadCount);
						// var count=_this.local_data.length/_this.$config.SysConfig.once_uploadCount
						// console.log('count:'+count);
						if(count==0){
							end="1"
							var data={
								data:JSON.stringify(_this.local_data),
								download:download,
								time:Math.round(new Date()/1000),
								end:end
							}
							// console.log(1);
							// console.log(data);
							_this.$http.syndata(data).then(res=>{
								//console.log(res);
								if(res.code=='1'){
									_this.$local.clearScanHistory(()=>{
										_this.local_data=[];
									},(e)=>{
										uni.showToast({
											title:_this.$config.msgs.clear_Fail,
											icon:'none',
											duration:1500
										})
										// uni.showToast(_this.$config.msgs.clear_Fail)
									})							
									
									_this.$local.insertLocalData(res.data,()=>{
										uni.hideLoading();
									},(e)=>{
										console.log(JSON.stringify(e))
									})
									//_this.$local.selectData()
									// uni.showToast(_this.$config.msgs.sync_OK)
									uni.showToast({
										title:_this.$config.msgs.sync_OK,
										icon:'none',
										duration:1500
									})
								}
								else{
									_this.$common.playErrBeep(1)
									uni.showToast({
										icon:'none',
										title:res.msg,
										duration:1500
									})
								}
							})
						}
						else{
							for(var i=0;i<count;i++){								
								// if((i*_this.$config.SysConfig.once_uploadCount+_this.$config.SysConfig.once_uploadCount)>_this.local_data.length){
								if((i*_this.$config.SysConfig.once_uploadCount+_this.$config.SysConfig.once_uploadCount)>=_this.local_data.length){
									end="1"
								}
								var data={
									data:JSON.stringify(_this.local_data.slice(i*_this.$config.SysConfig.once_uploadCount,i*_this.$config.SysConfig.once_uploadCount+_this.$config.SysConfig.once_uploadCount+1)),
									download:download,
									time:Math.round(new Date()/1000),
									end:end
								}
								// console.log('i='+i);
								// console.log('begin:'+(i*_this.$config.SysConfig.once_uploadCount).toString()+'end:'+(i*_this.$config.SysConfig.once_uploadCount+_this.$config.SysConfig.once_uploadCount+1).toString());
								// console.log(data);
								_this.$http.syndata(data).then(res=>{
									//console.log(res);
									if(res.code=='1'){
										if(end=="1"){
											_this.$local.clearScanHistory(()=>{
												_this.local_data=[];
												_this.$config.SysConfig.counter_offline=0;
											},(e)=>{
												// uni.showToast(_this.$config.msgs.clear_Fail)
												uni.showToast({
													title:_this.$config.msgs.clear_Fail,
													icon:'none',
													duration:1500
												})
											})
											
											_this.$local.insertLocalData(res.data,()=>{
												uni.hideLoading();
											},(e)=>{
												console.log(JSON.stringify(e))
											})
											// _this.$local.selectData()
											// uni.showToast(_this.$config.msgs.sync_OK)
											uni.showToast({
												title:_this.$config.msgs.sync_OK,
												icon:'none',
												duration:1500
											})
										}
									}
									else{
										_this.$common.playErrBeep(1)
										uni.showToast({
											icon:'none',
											title:res.msg,
											duration:1500
										})
									}
								})
							}
						}
						//uni.hideLoading();
					},()=>{
						// uni.showToast(this.$config.msgs.needwifi)
						uni.showToast({
							title:this.$config.msgs.needwifi,
							icon:'none',
							duration:1500
						})
					});				
				}
				else{
					// uni.showToast(this.$config.msgs.needonline)
					uni.showToast({
						title:this.$config.msgs.needonline,
						icon:'none',
						duration:1500
					})
				}
			}
		}
	}
</script>

<style>
.body{
	border-top: solid 1px #DBDBDA;
}
.menu-wrapper{
	flex-direction: row;
	display: flex;
}
.menu-wrapper .menu-title{
	color: #FFFFFF;
	background-color: #ADADAD;
	width: 100%;
	padding: 15upx 30upx;
}
.menu-wrapper .chk-wrapper{
	width: 100%;
	padding: 15upx 30upx;
}

.menu-wrapper .btn-wrapper{
	width: 100%;
	padding: 15upx 30upx;
}
.mitem-wrapper{
	flex: 1;
	justify-content: center;
	text-align: center;
	margin-top: 30upx;
	display: flex;
	flex-direction: column;
}

.mitem-wrapper .m_icowrapper{
	padding: 10upx 10upx 5upx 10upx;
}

.mitem-wrapper .m_ico{
	width: 120upx;
	height: 120upx;
}
.menu-wrapper-list{	
	justify-content: center;
	display: flex;
	flex-direction: row;
	width: 100%;
	border-bottom: solid 2upx #EEEEEE;
	padding: 10upx;
}
.menu-wrapper-list .seq-wrapper{
	display: flex;
	flex-direction: column;
	flex: 1;	
	line-height: 80upx;
}
.menu-wrapper-list .content-wrapper{
	display: flex;
	flex-direction: column;
	flex: 8;	
}

.menu-wrapper-list .date-wrapper{
	display: flex;
	flex-direction: column;
	flex: 3;	
}
.font-lg{
	font-size: 40upx;
}
.font-sm{
	font-size: 12upx;
}
.footer-wapper {
	position: fixed;
	bottom: 0;
	
	display: flex;
	flex-direction: column;
	width: 92%;
}

</style>
